﻿Partial Public Class MetaCommandExplorer

    Public aPL As SystemBoxSDK.PL.MetaSDK.MetaCommand_PL = Nothing


    Public Overridable Sub ProcessCurrentMasterRecord(ByVal sender As Object, ByVal e As DevExpress.XtraGrid.Views.Base.ValidateRowEventArgs)
        Throw New NotImplementedException
    End Sub


    Public Overridable Sub ValidateMasterRow(sender As Object, e As Views.Base.ValidateRowEventArgs)

    End Sub

    Public Overridable Sub ValidateDetailRow(sender As Object, e As Views.Base.ValidateRowEventArgs)
        Throw New NotImplementedException
    End Sub

    Public Overridable Sub InitNewMasterRow(sender As Object, e As InitNewRowEventArgs)
        Dim view As GridView = CType(sender, GridView)
        Dim row As DataRow = view.GetFocusedDataRow()
        Using MetaCommand As SystemBoxSDK.PL.MetaSDK.MetaCommand_PL = New SystemBoxSDK.PL.MetaSDK.MetaCommand_PL()
            MetaCommand.NewRecord(row)
        End Using

    End Sub

    Public Overridable Sub InitNewDetailRow(sender As Object, e As InitNewRowEventArgs)
        Throw New NotImplementedException
    End Sub

    Public Overridable Sub ProcessCurrentDetailRecord(sender As Object, e As Views.Base.ValidateRowEventArgs, index As Integer)
        Throw New NotImplementedException
    End Sub

    Public Overridable Sub DetailCellValueChanged(sender As Object, e As Views.Base.CellValueChangedEventArgs)
        Throw New NotImplementedException
    End Sub

    Public Overridable Sub MasterRowClick(sender As Object, e As RowClickEventArgs)
        Throw New NotImplementedException
    End Sub

    Public Overridable Sub DetailRowClick(sender As Object, e As RowClickEventArgs)
        Throw New NotImplementedException
    End Sub

    Public Overridable Sub DetailCustomRowCellEditForEditing(sender As Object, e As CustomRowCellEditEventArgs)
        Throw New NotImplementedException
    End Sub


    Public Shared Property MetaQueryDataRow As DataRow = Nothing

    ''' <summary>
    ''' ExportXsdCommand
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks>...</remarks>
    Public Shared Sub ExportXsdCommand(ByVal sender As Object, ByVal e As System.EventArgs)
        Dim aDataRow As DataRow = CType(CType(sender, DXMenuItem).Tag, DataRow)
        Dim queryID As Guid
        Using aFile As New System.Windows.Forms.SaveFileDialog
            aFile.DefaultExt = "xsd"
            aFile.FileName = "MetaXsd_" + aDataRow("QueryName").ToString
            aFile.AutoUpgradeEnabled = True
            If aFile.ShowDialog() = DialogResult.OK Then
                queryID = New Guid(aDataRow("QueryID").ToString)
                If MetaCommand_PL.ExportXsdCommand(aFile.FileName, queryID) Then
                    MessageWindow.ShowMessage("Export XSD Command Successful", MsgBoxStyle.Information)
                End If
            End If
        End Using
    End Sub

    ''' <summary>
    ''' ImportCommand
    ''' </summary>
    ''' <remarks>This SystemBOX function permit to import command XML File</remarks>
    Public Shared Sub ImportCommand()
        Dim saved As Boolean
        Using aFile As New System.Windows.Forms.OpenFileDialog
            aFile.FileName = "MetaQuery_*.xml"
            aFile.DefaultExt = "xml"
            aFile.Multiselect = True
            aFile.AutoUpgradeEnabled = True
            If aFile.ShowDialog() = DialogResult.OK Then
                ''''Me.Cursor = Cursors.WaitCursor
                For Each file As String In aFile.FileNames
                    saved = MetaCommand_PL.ImportMetaCommand(file)
                Next
                ''''Me.Cursor = Cursors.Default
                If saved Then
                    MessageWindow.ShowMessage("Import Command Successful", MsgBoxStyle.Information)
                End If
            End If
        End Using
    End Sub


    ''' <summary>
    ''' MenuCopyMetaCommand
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks>This SystemBOX function permit to copy command to a new COPY of this command called COPY_ ...</remarks>
    Public Shared Sub MenuCopyMetaCommand(ByVal sender As System.Object, ByVal e As EventArgs)
        Dim aDataRow As DataRow = CType(CType(sender, DXMenuItem).Tag, DataRow)
        Dim newName As String = String.Format("Copy of {0}", aDataRow("QueryName"))

        Try
            MetaCommand_PL.CopyMetaCommand(CType(aDataRow("QueryID"), Guid), newName)
            MessageWindow.ShowMessage(String.Format("Copy of '{0}' To '{1}' Done", aDataRow("QueryName"), newName), MsgBoxStyle.Information)
        Catch ex As Exception
            MessageWindow.ShowMessage(ex, MsgBoxStyle.Information)
        End Try
    End Sub

    ''' <summary>
    ''' MenuExportCommand
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks>This SystemBOX function permit to export Command to XML File</remarks>
    Public Sub MenuExportCommand(ByVal sender As System.Object, ByVal e As EventArgs)
        Dim aDataRow As DataRow = CType(CType(sender, DXMenuItem).Tag, DataRow)
        ExportCommand(aDataRow("QueryName").ToString, aDataRow("QueryID"))

    End Sub



    Private Sub TreeExportCommand()
        Try
            Dim atreelistSelection As TreeListMultiSelection = Me.Selection
            For Each anode As TreeListNode In atreelistSelection
                Console.WriteLine(anode("KeyID").ToString)
            Next
            If NodeDataRow IsNot Nothing Then
                ExportCommand(NodeDataRow("Description_PRM").ToString, NodeDataRow("KeyID"))

            End If
        Catch ex As Exception
            MessageWindow.ShowMessage(ex.Message)
        End Try

    End Sub



    Private Sub ExportCommand(ByVal QueryName As String, ByVal QueryID As Guid)

        Using aFile As New System.Windows.Forms.SaveFileDialog
            aFile.DefaultExt = "xml"
            aFile.FileName = "MetaQuery_" + QueryName
            aFile.AutoUpgradeEnabled = True
            If aFile.ShowDialog() = DialogResult.OK Then

                If MetaCommand_PL.ExportMetaCommand(aFile.FileName, QueryID) Then
                    MessageWindow.ShowMessage("Export Command Successful", MsgBoxStyle.Information)
                End If
            End If
        End Using

    End Sub


    ''' <summary>
    ''' MenuExportCommandToMaster
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks>This SystemBOX function permit to export Command to Master Database</remarks>
    Public Shared Sub MenuExportCommandToMaster(ByVal sender As System.Object, ByVal e As EventArgs)
        Dim aDataRow As DataRow = CType(CType(sender, DXMenuItem).Tag, DataRow)
        Dim queryID As Guid

        queryID = New Guid(aDataRow("QueryID").ToString)
        If MetaCommand_PL.ExportMetaCommandToMaster(queryID) Then
            MessageWindow.ShowMessage("Export Command Successful", MsgBoxStyle.Information)
        End If
    End Sub

    ''' <summary>
    ''' MenuGenerateColumn
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks>This SystemBOX function permit to generate column for a command</remarks>
    Public Shared Sub MenuGenerateColumn(ByVal sender As System.Object, ByVal e As EventArgs)
        Dim aDataRow As DataRow = CType(CType(sender, DXMenuItem).Tag, DataRow)
        If MetaCommand_PL.GenerateColumnsFromSelect(aDataRow("QueryName").ToString) Then
            MessageWindow.ShowMessage("Generate Columns Successful", MsgBoxStyle.Information)
        End If
    End Sub

    ''' <summary>
    ''' MenuGenerateColumn
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks>This SystemBOX function permit to generate column for a command</remarks>
    Public Shared Sub MenuGenerateParameterColumn(ByVal sender As System.Object, ByVal e As EventArgs)
        Dim aDataRow As DataRow = CType(CType(sender, DXMenuItem).Tag, DataRow)
        If MetaCommand_PL.GenerateColumnsFromSP(aDataRow("QueryName").ToString) Then
            MessageWindow.ShowMessage("Generate Columns Successful", MsgBoxStyle.Information)
        End If
    End Sub

    ''' <summary>
    ''' MenuGetRelationMetaCommand
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks>This SystemBOX function permit to generate Relation between Querie Command Father/Child</remarks>
    Public Shared Sub MenuGetRelationMetaCommand(ByVal sender As System.Object, ByVal e As EventArgs)
        Dim aDataRow As DataRow = CType(CType(sender, DXMenuItem).Tag, DataRow)
        Try
            MetaCommand_PL.GetRelationMetaCommand(CType(aDataRow("QueryID"), Guid), IIf(IsDBNull(aDataRow("ParentId_FK")), Nothing, aDataRow("ParentId_FK"))) ' CType(aDataRow("ParentId_FK"), Guid?))

            MessageWindow.ShowMessage("Get Relation Done", MsgBoxStyle.Information)
        Catch ex As Exception
            MessageWindow.ShowMessage(ex, MsgBoxStyle.Information)
        End Try
    End Sub

    ''' <summary>
    ''' MenuImportCommand
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks>This SystemBOX function launch the import of a Command</remarks>
    Public Shared Sub MenuImportCommand(ByVal sender As System.Object, ByVal e As EventArgs)
        ImportCommand()
    End Sub

    ''' <summary>
    ''' MenuPreviewCommand
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks>This SystemBOX function permit to see the result of a Query</remarks>
    Public Shared Sub MenuPreviewCommand(ByVal sender As System.Object, ByVal e As EventArgs)
        Dim aDataRow As DataRow = CType(CType(sender, DXMenuItem).Tag, DataRow)
        Dim aQueryName As String = aDataRow("QueryName").ToString
        Dim aTitle As String = String.Format("Preview of: '{0}'", aQueryName)
        Dim voidPL As SystemBoxSDK.PL.Void_PL = New SystemBoxSDK.PL.Void_PL(aQueryName)
        Dim Form As GridFormExtPL = New GridFormExtPL()

        ''''Me.Cursor = Cursors.WaitCursor
        Form.Text = aTitle
        Form.GridExt1.Pl = voidPL
        Form.IsReadOnlyGrid = False
        Form.Show()
        ''''Me.Cursor = Cursors.Default
    End Sub

    ''' <summary>
    ''' MenuPublishMetacommandToMetaControl
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks>
    ''' This SystemBOX function permit reverse engineering of Metacommand design process.
    ''' It will insert the MetaCommand created into the metacontrol table and assign a Sequnce ofr general Traceability
    ''' </remarks>
    Public Shared Sub MenuPublishMetacommandToMetaControl(ByVal sender As Object, ByVal e As EventArgs)
        Dim aDataRow As DataRow = CType(CType(sender, DXMenuItem).Tag, DataRow)
        Try
            'OUTSANDINGISSUE: Empecher de Publish une 2e fois ne fonctionne pas si on a pas fait un reload. clieng 2013-05-14.
            If IsDBNull(aDataRow("ControlID_FK")) Then
                MetaCommand_PL.PublishMetacommandToMetaControl(CType(aDataRow("QueryID"), Guid))
                MessageWindow.ShowMessage("Publish Done", MsgBoxStyle.Information)
            Else
                MessageWindow.ShowMessage("Already Published: " + aDataRow("MetaQuerieCD").ToString, MsgBoxStyle.Information)
            End If
        Catch ex As Exception
            MessageWindow.ShowMessage(ex, MsgBoxStyle.Information)
        End Try
    End Sub

    ''' <summary>
    ''' ACT_00021_MenuValidateCommand
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks>This SystemBOX function permit validate if the SQL Command is OK.</remarks>
    Public Shared Sub ACT_00021_MenuValidateCommand(ByVal sender As System.Object, ByVal e As EventArgs)
        Dim aDataRow As DataRow = CType(CType(sender, DXMenuItem).Tag, DataRow)
        Try

            aDataRow("CommandStatusID_FK") = MetaCommand_PL.ValidateSQLCommand(aDataRow("QueryName").ToString, aDataRow("CommandTypeID_FK"))
            MetaCommand_PL.ValidateQuery(aDataRow("QueryName").ToString)
            MessageWindow.ShowMessage("Query is OK", MsgBoxStyle.Information)

        Catch ex As Exception
            MessageWindow.ShowMessage(ex, MsgBoxStyle.Information)
        End Try
    End Sub

    ''' <summary>
    ''' MenuValidateMetaColumn
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks>This SystemBOX function permit to see the result of a Query</remarks>
    Public Shared Sub MenuValidateMetaColumn(ByVal sender As System.Object, ByVal e As EventArgs)
        Dim aDataRow As DataRow = CType(CType(sender, DXMenuItem).Tag, DataRow)
        Dim aQueryName As String = aDataRow("QueryName").ToString
        Dim aTitle As String = "Validate MetaColumn Not Present in Command"
        Dim voidPL As SystemBoxSDK.PL.Void_PL = New SystemBoxSDK.PL.Void_PL("audit_MetaColumnNotInCommand")
        Dim Form As GridFormExtPL = New GridFormExtPL()

        ''''Me.Cursor = Cursors.WaitCursor
        Form.Text = aTitle
        Form.GridExt1.Pl = voidPL
        Form.IsReadOnlyGrid = True
        Form.Show()
        ''''Me.Cursor = Cursors.Default
    End Sub

    ''' <summary>
    ''' MenuViewSQLStatement
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks>This SystemBOX function permit to have the SQL Query Statement.</remarks>
    Public Shared Sub MenuViewSQLStatement(ByVal sender As System.Object, ByVal e As EventArgs)
        Dim aDataRow As DataRow = CType(CType(sender, DXMenuItem).Tag, DataRow)
        Dim aQueryName As String = aDataRow("QueryName").ToString
        Dim aTitle As String = String.Format("SQL Statement of: '{0}'", aQueryName)
        'Hack 2: Parse the SQL statement by SQL Key Word
        ''''Me.Cursor = Cursors.WaitCursor
        Dim aMetaQuery As MetaQuery = MetaQuery.GetMetaQueryByName(aQueryName)
        Using Form As MessageWindow = New MessageWindow() With {.Text = aTitle, .Size = New System.Drawing.Size(600, 800), .MaximizeBox = True}
            MessageWindow.ShowMessage(aMetaQuery.SQLStatementParsed, MsgBoxStyle.Information)
            '''''Me.Cursor = Cursors.Default
        End Using
    End Sub

    ''' <summary>
    ''' MenuReOrderColumns
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks>This SystemBOX function permit to Reorder all Column with the actual order.</remarks>
    Public Shared Sub MenuReOrderColumns(ByVal sender As Object, ByVal e As EventArgs)
        Dim aDataRow As DataRow = CType(CType(sender, DXMenuItem).Tag, DataRow)

        Dim mc As MetaDataTable = New MetaDataTable ' the set of columns for that Metaquery
        Dim aQueryId As Guid
        aQueryId = CType(aDataRow("QueryID"), Guid)
        mc.QueryName = "MetaColumns"
        mc.GetMetaCommand()
        mc.MetaCommand.SQLWhere = String.Format("QueryId_FK = '{0}'", aQueryId.ToString)
        mc.FillBusinessApplicationData()

        MetaCommand_PL.ReorderColumns(mc)
        MessageWindow.ShowMessage("Reorder Columns Successful", MsgBoxStyle.Information)

    End Sub

    ''' <summary>
    ''' MenuSQLWindowCommand
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks>...</remarks>
    Public Shared Sub MenuSQLWindowCommand(ByVal sender As System.Object, ByVal e As EventArgs)
        Dim sqlWindow As WinFormExt.SQLWindow = New WinFormExt.SQLWindow()
        sqlWindow.Show()
    End Sub

    Public Shared Sub MenuPublishMetaUserColumn(ByVal sender As System.Object, ByVal e As EventArgs)
        MetaQueryDataRow = CType(CType(sender, DXMenuItem).Tag, DataRow)
        Dim newForm As XtraEditors.XtraForm = New XtraEditors.XtraForm() With {.Text = "Publish MetaColumns", .Width = 600}
        Dim gridSP As GridExtSP = New GridExtSP("SSPMetaUserColumnInsertByMetaQuery") With {.Dock = DockStyle.Fill}
        gridSP.SaveCMD = AddressOf PublishMetaUserColumn
        AddHandler gridSP.AfterLoad, AddressOf AddRowParameter
        newForm.Controls.Add(gridSP)
        newForm.ShowDialog()
    End Sub

    Public Shared Function PublishMetaUserColumn(ByVal aMetaDataTable As MetaDataTable) As Boolean
        Try
            aMetaDataTable.ApplyChanges(reload:=False)
            MessageWindow.ShowMessage("Publish To MetaUserColumn Done", MsgBoxStyle.Information)
            Return True
        Catch ex As Exception
            MessageWindow.ShowMessage(ex, MsgBoxStyle.Information)
        End Try
        Return False
    End Function

    Public Shared Sub AddRowParameter(sender As Object, e As System.EventArgs)
        Try
            Dim aGrid As GridExtSP = CType(sender, GridExtSP)

            Dim arow As DataRow = aGrid.MetaDataTable.NewRow()
            'assign value for the metacolumn here

            arow("@MetaqueryID") = MetaQueryDataRow("QueryID")
            aGrid.MetaDataTable.Rows.Add(arow)

        Catch ex As Exception
            MessageWindow.ShowMessage(ex, MsgBoxStyle.Information)
        End Try
    End Sub


    Private Sub InitNewMasterRowMetaCommand(sender As Object, e As InitNewRowEventArgs)
        Dim view As GridView = CType(sender, GridView)
        Dim row As DataRow = view.GetFocusedDataRow()
        aPL = EditPanePL
        aPL.NewRecord(row)
    End Sub


End Class
